<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>分类参数</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片试图区域 -->
    <el-card>
        <el-alert title="注意：只允许为第三级分类设置相关参数 "  show-icon type="warning" :closable ="false"></el-alert>
        <el-row>
            <el-col class="cat_opt">
                <span>选择商品分类：</span>
                <el-cascader
                  v-model="selectedcaseKeys"
                  :options="this.cateList"
                  :props="caseProps"
                  @change="handleChange"
                  clearable
                >
              </el-cascader>
            </el-col>
        </el-row>
        <el-tabs v-model="activeName" @tab-click="handleTabClick">
            <el-tab-pane label="动态参数" name="many">
                <el-button type="primary" size="mini" :disabled= 'isBtnDisabled' @click="popup('添加动态参数')">添加参数</el-button>
                <!-- 动态参数表格 -->
                <el-table :data="manyTableDate" border stripe>
                    <el-table-column type="expand">
                      <template slot-scope="scope">
                        <!-- 循环渲染标签 -->
                        <el-tag @close="handClose(idx,scope.row)" closable v-for="(item,idx) in scope.row.attr_vals" :key="idx">
                          {{item}}
                        </el-tag>
                        <!-- 添加标签 -->
                        <el-input
                          class="input-new-tag"
                          v-if="scope.row.inputVisible"
                          v-model="scope.row.inputValue"
                          ref="saveTagInput"
                          size="small"
                          @keyup.enter.native="handleInputConfirm(scope.row)"
                          @blur="handleInputConfirm(scope.row)"
                        >
                      </el-input>
                      <el-button v-else class="button-new-tag" size="small" @click="showInput(scope.row)">+ New Tag</el-button>
                      </template>
                    </el-table-column>
                    <el-table-column type="index"></el-table-column>
                    <el-table-column label="参数名称" prop="attr_name"></el-table-column>
                    <el-table-column label="操作">
                        <!-- <template slot-scope="scope"> -->
                            <template>
                            <el-button type="primary" icon="el-icon-edit" size="mini" @click="popup('修改动态参数')">编辑</el-button>
                            <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-tab-pane>
            <el-tab-pane label="静态属性" name="only">
                <el-button type="primary" size="mini" :disabled= 'isBtnDisabled'  @click="setinfo('添加静态属性')">添加属性</el-button>
                <!-- 静态参数表格 -->
                <el-table :data="onlyTableDate" border stripe>
                    <el-table-column type="expand"></el-table-column>
                    <el-table-column type="index"></el-table-column>
                    <el-table-column label="属性名称" prop="attr_name"></el-table-column>
                    <el-table-column label="操作">
                        <!-- <template slot-scope="scope"> -->
                        <template>
                            <el-button type="primary" icon="el-icon-edit" size="mini" @click="popup('修改静态属性')">编辑</el-button>
                            <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-tab-pane>
        </el-tabs>
    </el-card>

    <PopUpBox
    v-if="isShow"
    @change='getsishow'
    :tielename= tielename
    :addCase= addCase>
          <el-form :model="addForm" ref="addFormRef" label-width="100px">
            <el-form-item :label="this.tielename">
                <el-input v-model="addForm.attr_name"></el-input>
            </el-form-item>
        </el-form>
    </PopUpBox>
  </div>
</template>

<script>
import { instance } from '@/utils/http'
import PopUpBox from '@/views/PopUpBox '
export default {
  components: {
    PopUpBox
  },
  created () {
    this.getcaseList()
  },
  computed: {
    isBtnDisabled () {
      if (this.selectedcaseKeys.length !== 3) {
        return true
      } else { return false }
    },
    cateId () {
      if (this.selectedcaseKeys.length !== 3) {
        return Number(this.selectedcaseKeys[2])
      } else { return null }
    }
  },
  data () {
    return {
      // 弹出框的数据
      tielename: '',
      isShow: false,
      attrId: '',
      addForm: {},
      //
      cateList: [],
      caseProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      selectedcaseKeys: [],
      //   选项卡数据
      activeName: 'many',
      manyTableDate: [],
      onlyTableDate: []
    }
  },
  methods: {
    async getcaseList () {
      const { data: res } = await instance.get('categories')
      this.cateList = res.data
    },
    handleChange () {
      this.getParamsDate()
    },
    // 选项卡
    handleTabClick () {
      this.getParamsDate()
    },
    // 获取参数的列表数据
    async getParamsDate () {
      const { data: res } = await instance.get('categories/' + Number(this.cateId) + '/attributes', {
        params: { sel: this.activeName }
      })
      res.data.forEach(item => {
        item.inputVisible = false
        item.inputValue = ''
        item.attr_vals = item.attr_vals ? item.attr_vals.split(' ') : []
        // 控制文本框的显示与隐藏
      })
      // console.log(res.data)
      if (this.activeName === 'many') {
        this.manyTableDate = res.data
      } else {
        this.onlyTableDate = res.data
      }
    },
    // 弹出框
    getsishow () {
      this.isShow = !this.isShow
    },
    popup (tielename, attrId) {
      this.tielename = tielename
      this.isShow = !this.isShow
      this.attrId = attrId
      this.addForm = {}
    },
    async addCase () {
      const { data: res } = await instance.post('categories/' + Number(this.cateId) + '/attributes', {
        attr_name: this.addForm.attr_name,
        attr_sel: this.activeName
      })
      if (res.meta.status !== 201) {
        return this.$message.error('添加失败')
      } else {
        this.getsishow()
        this.getParamsDate()
        return this.$message.success('添加成功')
      }
    },
    // 展开tab
    async saveAttrVals (row) {
      const { data: res } = await instance.put('categories/' + Number(this.cateId) + '/attributes/' + row.attr_id, {
        attr_name: row.attr_name,
        attr_sel: row.attr_sel,
        attr_vals: row.attr_vals.join(' ')
      })
      if (res.meta.status !== 200) {
        return this.$message.error('修改参数失败')
      }
      this.$message.success('修改参数成功')
    },
    async handleInputConfirm (row) {
      if (row.inputValue.trim().length === 0) {
        row.inputVisible = ''
        row.inputVisible = false
        return
      }
      row.attr_vals.push(row.inputValue.trim())
      row.inputValue = ''
      row.inputVisible = false
      this.saveAttrVals(row)
    },
    showInput (row) {
      row.inputVisible = true
      this.$nextTick(() => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    handClose (idx, row) {
      row.attr_valsx.splice(idx, 1)
      this.saveAttrVals(row)
    }
  }
}
</script>

<style scoped lang="less">
.cat_opt{
    margin: 15px 0;
}
.input-new-tag{
  width: 120px;
}
</style>
